import { createRouter, createWebHistory } from 'vue-router'
import AnnouncementList from '../views/AnnouncementList.vue'
import HomePage from '../components/HomePage.vue'
import DailyReport from '../views/DailyReport.vue'
import Task from '../views/Task.vue'
import Login from '../views/Login.vue'
import Reply from '../views/Reply.vue'
import Alarm from '../views/Alarm.vue'
import Manager from '../views/Manager.vue'
import Employees from '../views/UserManager.vue'
import SiteManager from '../views/SiteManager.vue'
import ComponentManager from '../views/ComponentManager.vue'
import EquipmentManager from '../views/EquipmentManager.vue'
import ModuleManager from '../views/ModuleManager.vue'
import LabelManager from '../views/LabelManager.vue'
import DailyDetail from '../views/DailyDetail.vue'
import KnowledgeManager from '../views/KnowledgeManager.vue'
import Learning from '../views/Learning.vue'
import Sop from '../views/Sop.vue'
import Words from '../views/Words.vue'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            name: 'Home',
            component: HomePage,
            beforeEnter: async (to, from, next) => {
                try {
                    // 检查用户是否已登录
                    const realname = localStorage.getItem('realname')
                    if (!realname) {
                        next('/login')
                        return
                    }

                    next() // 如果一切正常，继续导航
                } catch (error) {
                    console.error('HomePage access error:', error)
                    next('/login') // 发生错误时重定向到登录页
                }
            }
        },
        {
            path: '/forum',
            name: 'forum',
            component: () => import('../views/Forum.vue'),
            beforeEnter: async (to, from, next) => {
                try {
                    // 检查用户是否已登录
                    const realname = localStorage.getItem('realname')
                    if (!realname) {
                        next('/login')
                        return
                    }

                    next() // 如果一切正常，继续导航
                } catch (error) {
                    console.error('HomePage access error:', error)
                    next('/login') // 发生错误时重定向到登录页
                }
            }
        },
        {
            path: '/task',
            name: 'task',
            component: Task,
            beforeEnter: async (to, from, next) => {
                try {
                    // 检查用户是否已登录
                    const realname = localStorage.getItem('realname')
                    if (!realname) {
                        next('/login')
                        return
                    }

                    next() // 如果一切正常，继续导航
                } catch (error) {
                    console.error('HomePage access error:', error)
                    next('/login') // 发生错误时重定向到登录页
                }
            }
        },
        {
            path: '/daily-report',
            name: 'daily-report',
            component: DailyReport,
            beforeEnter: async (to, from, next) => {
                try {
                    // 检查用户是否已登录
                    const realname = localStorage.getItem('realname')
                    if (!realname) {
                        next('/login')
                        return
                    }

                    next() // 如果一切正常，继续导航
                } catch (error) {
                    console.error('HomePage access error:', error)
                    next('/login') // 发生错误时重定向到登录页
                }
            }
        },
        {
            path: '/announcement/list/:id',
            name: 'AnnouncementList',
            component: AnnouncementList,
            beforeEnter: async (to, from, next) => {
                try {
                    // 检查用户是否已登录
                    const realname = localStorage.getItem('realname')
                    if (!realname) {
                        next('/login')
                        return
                    }

                    next() // 如果一切正常，继续导航
                } catch (error) {
                    console.error('HomePage access error:', error)
                    next('/login') // 发生错误时重定向到登录页
                }
            }
        },
        {
            path: '/announcement/:id',
            name: 'AnnouncementDetail',
            component: () => import('../views/AnnouncementDetail.vue'),
            beforeEnter: async (to, from, next) => {
                try {
                    // 检查用户是否已登录
                    const realname = localStorage.getItem('realname')
                    if (!realname) {
                        next('/login')
                        return
                    }

                    next() // 如果一切正常，继续导航
                } catch (error) {
                    console.error('HomePage access error:', error)
                    next('/login') // 发生错误时重定向到登录页
                }
            }
        },
        {
            path: '/login',
            name: 'login',
            component: Login
        },
        {
            path: '/reply/:id',
            name: 'Reply',
            component: Reply,
            meta: {
                requiresAuth: true
            },
            beforeEnter: async (to, from, next) => {
                try {
                    // 检查用户是否已登录
                    const realname = localStorage.getItem('realname')
                    if (!realname) {
                        next('/login')
                        return
                    }

                    next() // 如果一切正常，继续导航
                } catch (error) {
                    console.error('HomePage access error:', error)
                    next('/login') // 发生错误时重定向到登录页
                }
            }
        },
        {
            path: '/knowledge',
            component: KnowledgeManager,
            children: [
                {
                    path: 'learning',
                    component: Learning,
                    children: [
                        {
                            path: 'words',
                            component: Words
                        },
                        {
                            path: 'alarm',
                            component: Alarm
                        }
                    ]
                },
                {
                    path: 'sop',
                    component: Sop,
                    children: [
                        {
                            path: 'lesson/:id',
                            name: 'LessonDetail',
                            component: () => import('../views/LessonDetailView.vue'),
                            meta: {
                                title: '课程详情',
                                requireAuth: true
                            }
                        }
                    ]
                }
            ]
        },
        {
            path: '/analysis',
            component: () => import('../views/AnalysisManager.vue'),
            children: [
                {
                    path: 'profiler1d',
                    component: () => import('../views/analysis/Profiler1DView.vue')
                },
                {
                    path: 'profiler2d',
                    component: () => import('../views/analysis/Profiler2DView.vue')
                },
                {
                    path: 'profiler',
                    component: () => import('../views/analysis/ProfilerView.vue')
                },
                {
                    path: 'position',
                    component: () => import('../views/analysis/PositionLogView.vue')
                },
                {
                    path: '',
                    redirect: 'uniformity'
                }
            ]
        },
        {
            path: '/user/personal',
            name: 'Personal',
            component: () => import('../views/Personal.vue')
        },
        {
            path: '/admin/manager',
            component: Manager,
            children: [
                {
                    path: '/admin/employees',
                    component: Employees
                },
                {
                    path: '/admin/site',
                    component: SiteManager
                },
                {
                    path: '/admin/component',
                    component: ComponentManager
                },
                {
                    path: '/admin/equipment',
                    component: EquipmentManager
                },
                {
                    path: '/admin/module',
                    component: ModuleManager
                },
                {
                    path: '/admin/label',
                    component: LabelManager
                }
            ]
        },
        {
            path: '/daily-detail/:id',
            name: 'DailyDetail',
            component: DailyDetail
        }
    ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
    // 定义不需要登录的白名单路径
    const whiteList = ['/login', '/daily-detail']
  
    // 检查当前路径是否在白名单中
    const isWhiteListed = whiteList.some(path => to.path.startsWith(path))
    
    if (isWhiteListed) {
        next()
        return
    }
    
    // 检查用户是否已登录
    const realname = localStorage.getItem('realname')
    if (!realname) {
        next('/login')
        return
    }
    
    next()
})

export default router

const handleViewDetail = (lesson) => {
  router.push(`/lesson/${lesson.id}`)
}